// credits/components/exchange-modal/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        creditsType: {
            type: Object,
            default: {}
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        credits: 0,
        score:0,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onCreditsChange(event) {
            this.setData({
                credits: event.detail.value
            })
        },
        onSubmit() {
            const creditsType = this.data.creditsType;
            let score = this.data.credits
    

            if(score <= 0) {
                wx.showToast({
                  title: '请输入正确的积分',
                })
                return
            }
            wx.ajax.post('dhScore.action', { type: creditsType.type, score }, true).then(res => {
               if(res.success) {
                 wx.showToast({
                    title: creditsType.type == '3' ? '提现成功' : '兑换成功',
                  })
                  this.triggerEvent('close', { refresh: true})
               } else {
                 wx.showToast({
                    title: creditsType.type == '3' ? '超过可提现积分' : '超过可兑换积分',
                  })
               }

            })
        },
        closeModal() {
            this.triggerEvent('close', { refresh: false })
        }
    },
    lifetimes: {
        attached: function () {
            console.log(this.data.creditsType)
            this.setData({
                credits: this.data.creditsType.total,
                score: this.data.creditsType.total,
            })
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },
})